<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework					|
 * | Version 1.0								|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>			|
 * | Blog: http://mcezzare.blogspot.com.br					|
 * |										|
 * | This program is part of PHPKiisf, and is free software: you can		|
 * | redistribute it and/or modify it under the terms of the GNU 		|
 * | General Public License as published by					|
 * | the Free Software Foundation, either version 3 of the License, or		|
 * | (at your option) any later version.					|					|
 * |										|									|
 * | This program is distributed in the hope that it will be useful,		|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of		|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		|
 * | GNU General Public License for more details.				|
 * | You should have received a copy of the GNU General Public License		|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.	|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>		|
 * +----------------------------------------------------------------------------+
 * | Contributors								|
 * |										|
 * +----------------------------------------------------------------------------+
 *
 */
/*
 * @package	Controllers
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * GUI controller class of framework.
 *
 * Classe used to create html gui objects
 * Why not a form or layout controller, cause I think
 * a single class can handle this job
 *
 */
class gui_controller extends default_controller {

	public static $buttons ;

	/**
	 *  DIVS SECTION
	 */

	/**
	 *
	 * @param type $params
	 *
	 */
	public static function print_div_pre_message( $message , $params = null ) {
		$color = "#000000" ;
		$bg_color = "#f5f5f5" ;
		if ( !$message instanceof message ) {
			$message_aux = utils::prepare_message( $message ) ;
			$message = $message_aux ;
		}
		if ( isset( $message->msg_type ) ) {
			$style = utils::$message_style{$message->msg_type} ;
			$color = $style{"color"} ;
			$bg_color = $style{"bgcolor"} ;
			$img_class_icon = $style{"class_icon"} ;
		}
		$div = "<div style='border:solid 1px #EA9C5C;  padding:3px; color:$color; background-color:$bg_color'>
						<pre>
							<img class=\"$img_class_icon\" />"
			. utils::object_to_string( $message ) .
			"</pre>
				</div>" ;

		require_once base_template::$skin_files{"header"}{"filename"} ;
		require_once base_template::$skin_files{"navbar"}{"filename"} ;
		print( $div ) ;
		require_once base_template::$skin_files{"footer"}{"filename"} ;
		return ;
	}

	/**
	 * [print_div_results description]
	 * @param  [type] $message [description]
	 * @param  [type] $params  [description]
	 * @return [type]          [description]
	 */
	public static function print_div_results( $message , $params = null , $force_br = null ) {
		if ( $message === null ) {
			return ;
		}
		$force_br_tag = "" ;
		if ( $force_br ) {
			$force_br_tag = "<br>" ;
		}
// now we need just an array
		$base_div = <<<EOT
				<div class="underline_%s" style="position: absolute;right: 30%s;">
						<img class="icon-thumbs-%s" />%s
				</div>
EOT;

		$sufix_class_span = "err" ;
		$sufix_class_img = "down" ;
		if ( $message{"RESULT"} == true ) {
			$sufix_class_span = "ok" ;
			$sufix_class_img = "up" ;
		}
		$div = sprintf( $base_div , $sufix_class_span , "%" , $sufix_class_img , $message{"MESSAGE"} ) ;

		print(trim( $div ) ) ;
		return ;
	}

	/**
	 * Create Html Objects
	 *
	 * @param array $object
	 */
	public static function create_object( $object , $print = false ) {
		$html_object = null ;
		if ( is_array( $object ) ) {
			if ( isset( $object{"object"} ) ) {
// verify object type
//								echo "Creating " . $object{"object"};
				switch ( $object{"object"} ) {
					case "select":
						$html_object = sprintf( "%s" , self::create_select( $object , $print ) ) ;
						break ;
					case "textfield":
						$html_object = "" ;
						break ;
					case "button":
						$html_object = sprintf( "%s" , self::create_button( $object , $print ) ) ;
						break ;
					case "radio":
						$html_object = "" ;
						break ;
					case "checkbox":
						$html_object = "" ;
						break ;
					default :
						$html_object = "" ;
						break ;
				}
			}
		}
		return $html_object ;
	}

	/**
	 *  BUTTONS SECTION
	 */

	/**
	 *
	 * @param array $html_params
	 */
	public static function create_button( $html_params = null , $print = true ) {
		$button = "<a class=\"btn\" href=\"#\" data-text=\"View details &raquo;\">View details &raquo;</a>" ;
		$value_text = "View details &raquo;" ;


		if ( is_array( $html_params ) && $html_params !== null ) {
// bind value to text and self remove from the array
			if ( isset( $html_params{"value"} ) ) {
				$value_text = $html_params{"value"} ;
				unset( $html_params{"value"} ) ;
			}
			if ( !isset( $html_params{"class"} ) ) {
				$html_params{"class"} = "btn" ;
			}

			$button = sprintf( "<p><a %s data-text=\"$value_text\">$value_text</a></p>" , utils::array_to_params( $html_params ) ) ;
		}

		if ( $print ) {
			print( $button ) ;
		}

		return $button ;
	}

	/**
	 * Parent Base for form
	 *
	 * @param array $html_params form properties
	 * @param array $fields form fields
	 * @param boolean $from_model if will load from Model Object
	 */
	public static function create_form( $form_params , $fields = null , $from_model = false ) {
//sort by index key
		sort( $fields ) ;
		if ( $fields ) {
//start form
			printf( "\n<form %s>\n" , utils::array_to_params( $form_params ) ) ;
// print fields
			while ( list($key , $field) = each( $fields ) ) {
				/*
				 * involke create_object method
				 * normally called with print (2º) parameter false inside this mehod
				 * because prinf bellow will output the code
				 */

				printf( "%s\n" , self::create_object( $field , false ) ) ;
			}
			print "\n</form>\n" ;
		}
	}

	/**
	 *
	 * @param type $select
	 * @param type $print
	 * @param type $selected_value
	 * @return type
	 */
	public static function create_select( $select = null , $print = false , $selected_value = null ) {
		$return_select = null ;
		$select_base = "\n
				<select data-text=\"%s\" %s>
						%s
				</select>\n" ;

// avoid warnings and errors
		if ( is_array( $select ) && $select !== null ) {

			$return_select = sprintf(
				$select_base , $select{"name"} , utils::array_to_params( $select ) , utils::array_to_params( $select{"content"} , "select" )
				) ;
		}
		if ( $print ) {
			print $return_select ;
		}

		return $return_select ;
	}

	/**
	 *
	 * @param type $array
	 * @param type $print
	 * @param type $vertical
	 * @return string
	 */
	public static function create_table( $array , $print = true , $vertical = false ) {
		$table = null ;
		if ( is_array( $array ) ) {
			if ( $vertical ) {
				$table_header = "
				<table border=\"1\" cellspacing=\"4\" cellpadding=\"3\" style=\"font-size: 50%%\">
						<thead>
								<tr>
									<th>%s</th>
								</tr>
						</thead>
						<tbody>" ;
				$table_body = "
								<tr>
									<td>%s</td>
								</tr>" ;
				$table_footer = "
						</tbody>
				</table>\n" ;

				$table = sprintf( $table_header , implode( '</th><th>' , array_keys( current( $array ) ) ) ) ;
				foreach ( $array as $row ) {
					$table .= sprintf( $table_body , implode( '</td><td>' , $row ) ) ;
				}
				$table .= $table_footer ;
			} else {
				$table_header = "
				<table border=\"1\" cellspacing=\"4\" cellpadding=\"3\">" ;
				$table_body = "%s" ;
				$table_footer = "
				</table>\n" ;
				$table = $table_header ;
				foreach ( $array as $row => $value ) {
					foreach ( $value as $key => $val ) {
						$table .= sprintf( "<tr><th>%s</th><td>%s</td></tr>" , $key , $val ) ;
					}
				}
			}
			$table .= $table_footer ;
		}

		if ( $print ) {
			print( $table ) ;
			return ;
		}

		return $table ;
	}

}
?>